<template>
	<view class="cartaint">
		<!-- 手机号注册弹窗 -->
		<view class="popup">
			<u-popup v-model="showMoblie" mode="bottom" border-radius='50'>
				<view style="margin: 20rpx;">首次登录需要获取手机号</view>
				<button style='margin: 150rpx auto; width: 500rpx;' type="default" open-type="getPhoneNumber"
					@getphonenumber="decryptPhoneNumber">获取手机号</button>
			</u-popup>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showMoblie: false
			}
		},
		methods: {
			getUserProfile() {
				var that = this;
				return new Promise((resolve, reject) => {
					uni.getUserProfile({
						desc: '获取您的微信信息以授权小程序',
						success: userRes => {
							// console.log('getUserProfile-res', userRes);
							resolve(userRes);
							that.UserProfileRes = userRes;
						},
						fail: userErr => {
							uni.showToast({
								title: '授权失败',
								icon: 'error'
							});
							// console.log('getUserProfile-err', userErr);
							reject(userErr);
						}
					});
				});
			},
			getLoginCode() {
				var that = this;
				return new Promise((resolve, reject) => {
					uni.login({
						provider: 'weixin',
						success: loginRes => {
							// console.log('loginRes', loginRes);
							that.loginRes = loginRes;
							resolve(loginRes);
						}
					});
				});
			},
			login() {
				var that = this;
				uni.showLoading({
					title: '加载中'
				});
				let userProFile = this.getUserProfile();
				let loginCode = this.getLoginCode();
				loginCode.then(code => {
						that.code = code.code;
						return code;
					}).then(logCode => {
						// console.log(33, logCode)
						return new Promise((resolve, reject) => {
							userProFile.then(res => {
									that.rawData = res.rawData;
									uni.hideLoading();
									that.toGetData(res.userInfo)
								})
								.catch(err => {
									reject(err);
								});
						});
					})
					.then(res => {
						console.log('promise-res', res);
					})
					.catch(err => {
						console.log('userProfile-err', err);
					});
			},
			// 请求数据
			toGetData(userinfo) {
				const that = this
				let data = {
					code: this.code,
					rawData: this.rawData,
				}
				this.$u.api.login(data).then(res => {
					if (res.code === 1) {
						that.$store.commit('setToken', res.data.token)
						uni.setStorageSync('token', res.data.token)
						if (res.data.mobile) {
							uni.showToast({
								title: '登录成功',
								duration: 1000
							})
						} else {
							that.showMoblie = true;
						}
						this.getUsrInfo();
					}
				})
			},
			// 获取用户信息
			getUsrInfo() {
				this.$u.api.getUserInfo().then(res => {
					this.$store.commit('setUserInfo', res.data);
					uni.setStorageSync('userinfo', res.data);
					this.$emit('user', res.data);
				})
			},
			// 手机号注册
			decryptPhoneNumber(e) {
				// console.log(666, e)
				let data = {
					...e.detail
				}
				this.$u.api.getUserMobile(data).then(res => {
					if (res.code === 1) {
						this.showMoblie = false;
					}
				})
			},
		}
	}
</script>

<style>
</style>
